<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class Home_userController extends Controller
{
      //用户添加
      public function getAdd(){
          //引入添加模板
          return view('admin.home_user.add');
      }

      //用户插入操作
      public function postInsert(Request $request){

          //信息压入数组
          $arr = array();

          //去除_token 和repassword
          $arr = $request->except('_token','repassword');

          //token随机五十位
          $arr['token'] = str_random('50');

          //时间
          $arr['creatTime'] = date('Y-m-d H:i:s',time());

          //哈希解密
          $arr['h_upassword'] = Hash::make($arr['h_upassword']);

          //验证数据
          $this->validate($request, [
              'h_uname' => 'required',
              'h_upassword'=>'required',
              'repassword'=>'required|same:h_upassword',
              'h_uemail'=>'required|email',

          ],[
              'h_uname.required'=>'用户名必填',
              'h_upassword.required'=>'密码不能为空',
              'repassword.required'=>'确认密码必填',
              'repassword.same'=>'密码不一致',
              'h_uemail.required'=>'邮箱必填',
              'h_uemail.email'=>'邮箱格式不正确',
          ]);

          //获取插入数据
          $res=DB::table('home_user')->insert($arr);
        
          //判断结果
          if($res){
              return redirect('/admin/home_user/index')->with('success','插入成功');
          }else{
              return back()->with('error','插入失败');
          }
       }

       //用户列表页
       public function getIndex(Request $request){

          //获取搜索信息
          $search = $request->input('search');

          //获取分页id
          $num = $request->input('num',10);
          
          //判断搜索  模糊查询
          if(!empty($search)){
               $data = DB::table('home_user')->where('h_uname','like','%'.$search.'%')->paginate($num);
          }else{
              $data = DB::table('home_user')->paginate($num);
          }  
          return view('admin.home_user.index',['data'=>$data,'request'=>$request->all()]);
       }
       //用户删除
       public function getDelete(Request $request){

            //要删除的信息
            $data = $request->input('id');

            //获取删除的数据
            $res = DB::table('home_user')->where('h_uid',$data)->delete();

            //判断结果
            if($res>0){
                return redirect('/admin/home_user/index')->with('success','删除成功');
            }else{
                return back()->with('error','删除失败');
            }
       }

       //用户的修改液
       public function getEdit(Request $request){

            //获取修改id
            $res = $request->input('id');

            //获取修改数据
            $data = DB::table('home_user')->where('h_uid',$res)->first();

            //引入模板  传输数据
            return view('admin.home_user.edit',['data'=>$data]);
       }

       //修改操作
       public function postUpdate(Request $request){

          //获取修改id
          $res1 = $request->input('id');

          //去除_token  id
          $data=$request->except('_token','id');
          
          //id转换
          $data['h_uid'] = $res1;

          //获取修改数据
          $res = DB::table('home_user')->where('h_uid',$res1)->update($data);

          //判断结果
          if($res>0){
                  return redirect('/admin/home_user/index')->with('success','修改成功');
              }else{
                  return back()->with('error','修改失败');
              }
       }

       //权限操作 升权
       public function getUp(Request $request){
          //获取升权id
          $data = $request->input('id');

          //获取升权数据
          $res = DB::table('home_user')->where('h_uid',$data)->first();

          //判断状态  
          if($res['h_uauth']== 1){

            $res2 = DB::table('home_user')->where('h_uid',$data)->update(['h_uauth'=>2]); 
            
            return back()->with('success','升权成功'); 
          }else{
            return back()->with('error','升权失败');
          }
          
            
       }
       //降权操作
       public function getDown(Request $request){

          //获取降权id
          $data = $request->input('id');

          //获取数据
          $res = DB::table('home_user')->where('h_uid',$data)->first();

          //判断结果
          if($res['h_uauth']== 2){
            
                
                $res2 = DB::table('home_user')->where('h_uid',$data)->update(['h_uauth'=>1]);
               return back()->with('success','降权成功'); 
          }else{
            return back()->with('error','降权失败');
        }
      }
}      